<?php
require_once (CORE_PATH."api.php");
require_once (MODEL_PATH . "DAO/connection_mysql.php");
require_once (MODEL_PATH . "DAO/dao_interface.php");
require_once ("artist.php");
class Song implements DAOInterface {
	private $id;
	private $title;
	private $alias;
	private $intro;
	private $lyrics_chords;
	private $things_remembered;
	private $photo;
	private $tags;
	private $artist;
	
	public function getId(){
		return $this->id;
	}	
	public function setId($songId) {
		$this->id = $songId;
	}
	
	public function getTitle() {
		return $this->title;
	}
	public function setTitle($songTitle) {
		$this->title = $songTitle;
	}
	
	public function getAlias() {
		return $this->alias;
	}
	public function setAlias($songAlias) {
		$this->alias = $songAlias;
	}
	
	public function getIntro() {
		return $this->intro;
	}
	public function setIntro($songIntro) {
		$this->intro = $songIntro;
	}
	
	public function getLyricsChords() {
		return $this->lyrics_chords;
	}	
	public function setLyricsChords($song) {
		$this->lyrics_chords = $song;
	}
	
	public function getThingsRemembered() {
		return $this->things_remembered;
	}
	public function setThingsRemembered($thingsRemembered) {
		$this->things_remembered = $thingsRemembered;
	}
	
	public function getPhoto() {
		return $this->photo;
	}
	public function setPhoto($path) {
		$this->photo = $path;
	}
	
	public function getTags() {
		return $this->tags;
	}
	public function setTags($songTags) {
		$this->tags = $songTags;
	}
	
	public function getArtist() {
		return $this->artist;
	}
	public function setArtist($anArtist) {
		$this->artist = $anArtist;
	}
	
	public function __construct() {
		$this->title = "";
		$this->alias = "";
		$this->intro = "";
		$this->lyrics_chords = "";
		$this->things_remembered = "";
		$this->photo = "";
		$this->tags = "";
		$this->artist = new Artist();
	}
	/*
	 //MULTI CONSTRUCTORS
		function __construct() {
	        $argv = func_get_args();
	        switch( func_num_args() ) {
	            case 1:
	                self::__construct1($argv[0]);
	                break;
	            case 2:
	                self::__construct2( $argv[0], $argv[1] );
	                break;
	         }
	    }
	 
	    function __construct1($arg1) {
	    ...
	    }
	 
	    function __construct2($arg1, $arg2) {
	    ... 
	    }
	 */
	public function setInfo($songTitle, $songIntro, $song, $thingsRemembered, $songTags, $imagePath, $anArtist) {
		$this->setTitle($songTitle);
		$alias = utf8convert($songTitle);
		$alias = str_replace(" ", "_", $alias);
		$this->setAlias($alias);
		$this->setIntro($songIntro);
		$this->setLyricsChords($song);
		$this->setThingsRemembered($thingsRemembered);
		$this->setTags($songTags);
		$this->setPhoto($imagePath);
		$this->setArtist($anArtist);
	}
	
	public function add() {		
		$conn = new ConnectionMySQL();
		$conn->connect();
		
		//if this artist is not exist in database
		$artistId = $this->artist->isExist();
		if($artistId == -1) {
			$this->artist->add();
		} else {
			$this->artist->setId($artistId);
		}
		
		$sqlFormat = "INSERT INTO song (title, alias, intro, lyrics_chords, 
						things_remembered, photo, tags, artist)
					VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', %d);";
		$query = sprintf($sqlFormat, 
			$conn->escape_string($this->getTitle()),
			$this->getAlias(),
			$conn->escape_string($this->getIntro()),
			$conn->escape_string($this->getLyricsChords()),
			$conn->escape_string($this->getThingsRemembered()),
			$conn->escape_string($this->getPhoto()),			
			$conn->escape_string($this->getTags()),
			$conn->escape_string($this->getArtist()->getId()));

		
		$conn->query($query);	
		$this->setId($conn->last_insert_id());
	
		$conn->disconnect();
	}	
	
	public function delete() {
		
	}
	
	public function update() {
		
	}
	
	public function getAll() {
		
	}
	
	public static function getSongByAlias($songAlias){		
		$song = null;
		$conn = new ConnectionMySQL();
		$conn->connect();
		
		$sqlFormat = "SELECT * FROM song WHERE alias='%s';";		
		$query = sprintf($sqlFormat, 
				$conn->escape_string($songAlias));
		$result = $conn->query($query);	
		$obj = $conn->get_object_returned($result);		
		if($obj != NULL) {
			$song = new Song();
			$song->setId($obj["id"]);
			$song->setTitle($obj["title"]);
			$song->setIntro($obj["intro"]);
			$song->setLyricsChords($obj["lyrics_chords"]);
			$song->setThingsRemembered($obj["things_remembered"]);
			$song->setTags($obj["tags"]);
			if($obj["photo"] != "") {			
				$song->setPhoto(W3C_PATH."resources/photo/song/".$obj["photo"]);
			} else {
				$song->setPhoto(W3C_PATH."resources/photo/song/default.png");
			}
			
			$artistId = $obj["artist"];	 		
			$song->setArtist(Artist::getArtistById($artistId));
		}
		$conn->disconnect();
		return $song;
	}
	
	function getChords() {
		$arrChords = null;
		preg_match_all('/[A-Za-z1-9#b]+[\-]|[\-][A-Za-z1-9#b]+/', $this->getLyricsChords(), $arrMatches);
		
		$arrChords = $arrMatches[0];
		$len = count($arrChords);
		for($i=0; $i<$len; $i++) {
			$arrChords[$i] = str_replace("-", "", $arrChords[$i]);
		}
		
		$arrChords = array_unique($arrChords);
		
		return $arrChords;
	}
	
	function getLyricsChordsHTML() {
		$chords = $this->getChords();	

		$html = $this->getLyricsChords();
		foreach ($chords as $chord) {
		$reg_ex = '/'.$chord.'\-|\-'.$chord.'/';
		$html = preg_replace(
			$reg_ex,						
			'<b>' . $chord . '</b>', 
			$html);
		}
				
		return $html;
	}
}




